//
//  DigitAlertPrompt.m
//  sy_pad_l
//
//  Created by Chiu Chi Ho on 3/3/13.
//  Copyright (c) 2013 Chiu Chi Ho. All rights reserved.
//

#import "DigitAlertPrompt.h"
#import "PreferenceManager.h"
#import "TimeslotManager.h"

@implementation DigitAlertPrompt
@synthesize textField;
@synthesize enteredText;

//- (id)initWithTitle:(NSString *)title message:(NSString *)message delegate:(id)delegate cancelButtonTitle:(NSString *)cancelButtonTitle okButtonTitle:(NSString *)okayButtonTitle
//{
//	
//    if (self = [super initWithTitle:title message:message delegate:delegate cancelButtonTitle:cancelButtonTitle otherButtonTitles:okayButtonTitle, nil])
//    {
//        UITextField *theTextField = [[UITextField alloc] initWithFrame:CGRectMake(12.0, 45.0, 260.0, 25.0)];
//        [theTextField setBackgroundColor:[UIColor whiteColor]];
//        [self addSubview:theTextField];
//		
//        self.textField = theTextField;
//        [theTextField release];
//		
////        CGAffineTransform translate = CGAffineTransformMakeTranslation(0.0, 130.0);
////        [self setTransform:translate];
//    }
//    return self;
//}

- (id)initWithForceInput:(BOOL)isForce
{
	NSString *title = NSLocalizedString(@"Enter 3rd digit", @"");
	NSString *message = @"\r\n";
	NSString *okTitle = NSLocalizedString(@"OK", @"");
	
	NSString *cancelTitle = nil;
	if(!isForce)	//Allow Cancel
		cancelTitle = NSLocalizedString(@"Cancel", @"");
	
    if (self = [super initWithTitle:title message:message delegate:self cancelButtonTitle:cancelTitle otherButtonTitles:okTitle, nil])
    {
        UITextField *theTextField = [[UITextField alloc] initWithFrame:CGRectMake(12.0, 45.0, 260.0, 25.0)];
        [theTextField setBackgroundColor:[UIColor whiteColor]];
        [self addSubview:theTextField];
		
        self.textField = theTextField;
        [theTextField release];
    }
    return self;
}

- (void)show
{
    [textField becomeFirstResponder];
    [super show];
}

- (NSString *)enteredText
{
    return textField.text;
}

- (void)dealloc
{
    [textField release];
    [super dealloc];
}

- (BOOL)isEnteredTextValid:(NSString *)value
{
	if([value length] != 1)
		return NO;
	
	NSCharacterSet *alphaNumbersSet = [NSCharacterSet decimalDigitCharacterSet];

	NSCharacterSet *stringSet = [NSCharacterSet characterSetWithCharactersInString:value];
	BOOL isNumber = [alphaNumbersSet isSupersetOfSet:stringSet];

	if(!isNumber)
		return NO;
	
	return YES;
}

- (void)dismissWithClickedButtonIndex:(NSInteger)buttonIndex animated:(BOOL)animated
{
	NSString *value = [[self enteredText] uppercaseString];
	
	if([self isEnteredTextValid:value])
	{
		NSInteger digit = [value integerValue];
		NSString *groupId = [TimeslotManager convertToGroupByDigit:digit];	//Get the group id
		
		NSString *message = [NSString stringWithFormat:@"%@ %@", NSLocalizedString(@"Your group is", @""), groupId];
		
		UIAlertView *alert = [[UIAlertView alloc] initWithTitle:NSLocalizedString(@"Record saved", @"") message:message delegate:nil cancelButtonTitle:nil otherButtonTitles:@"OK", nil];
		alert.tag = 999;
		[alert show];
		[alert release];
		
		//Save the third digit
		PreferenceManager *mgr = [PreferenceManager sharedManager];
		[mgr saveThirdDigit:digit];
		
		[[NSNotificationCenter defaultCenter] postNotificationName:@"GroupIdDidChange"
															object:nil
														  userInfo:[NSDictionary dictionaryWithObject:groupId forKey:@"Group_Id"]];
		
		[super dismissWithClickedButtonIndex:buttonIndex animated:animated];
	}
	else
	{
		[textField resignFirstResponder];
		
		UIAlertView *alert = [[UIAlertView alloc] initWithTitle:NSLocalizedString(@"Invalid input", @"") message:nil delegate:self cancelButtonTitle:nil otherButtonTitles:@"OK", nil];
		alert.tag = 1;
		[alert show];
		[alert release];
	}
	
}

#pragma mark - UIAlertViewDelegate
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
	if(alertView.tag == 1)	//Invalid Input alert
	{
		[textField becomeFirstResponder];
	}
}


@end
